﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.SQLite;
using Com.Toook.Common;
using System.Text.RegularExpressions;

namespace System.Web.UI
{
    /// <summary>
    ///BasePage 的摘要说明
    /// </summary>
    public class BaseShowPage : System.Web.UI.Page
    {
        public BaseShowPage()
        {
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (ConfigurationManager.AppSettings["IsSaveUserVisitRecord"] == "1")
            {
                SaveVisitRecord();
            }
        }
        private bool IsValidVisit()
        {
            bool isValid = true;
            string agent = Request.UserAgent;
            for (int i = 0; i < 10; i++)
            {
                if (IsSpider(agent))
                {
                    return true;
                }
            }

            string path = Request.Path;
            string type = Request["type"];
            string page = Request["page"];

            return isValid;
        }
        private bool IsSpider(string agent)
        {
            bool engine = false;
            if (!string.IsNullOrEmpty(agent))
            {
                Regex rx = new Regex(ConfigurationManager.AppSettings["IsSpider"], RegexOptions.IgnoreCase | RegexOptions.Compiled);
                engine = rx.IsMatch(agent);
            }
            return engine;
        }
        /// <summary>
        /// 保存用户访问记录
        /// </summary>
        private void SaveVisitRecord()
        {
            string url = Request.RawUrl;
            string ip = Request.UserHostAddress;
            string refer = Request.UrlReferrer == null ? "no" : Request.UrlReferrer.ToString();
            string agent = Request.UserAgent;
            string command = "insert into UserVisitRecord(ip, agent, url, refer) "
                    + " values(@ip, @agent, @url, @refer) ";
            SQLiteConnection con = SqliteHelper.GetConnection("SQLiteDatabase");
            SQLiteCommand cmd = new SQLiteCommand(command, con);
            cmd.Parameters.Add("@ip", System.Data.DbType.String, 15).Value = ip;
            cmd.Parameters.Add("@agent", System.Data.DbType.String, 200).Value = agent.Length > 200 ? agent.Substring(0, 200) : agent;
            cmd.Parameters.Add("@url", System.Data.DbType.String, 200).Value = url;
            cmd.Parameters.Add("@refer", System.Data.DbType.String, 500).Value = refer;

            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                LogHelper.Log.Error("SaveVisitRecord保存访问记录错误", ex);
            }
            finally
            {
                SqliteHelper.CloseDbResource(con, null);
            }

        }
    }
}